package com.zenith.edu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zenith.edu.mapper.EduForumReplyMapper;
import com.zenith.edu.po.domain.EduForumReply;
import com.zenith.edu.po.dto.EduForumReplyDTO;
import com.zenith.edu.po.vo.EduForumReplyVO;
import com.zenith.edu.service.EduForumReplyService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class EduForumReplyServiceImpl extends ServiceImpl<EduForumReplyMapper, EduForumReply> implements EduForumReplyService {
    
    @Override
    public EduForumReplyVO createForumReply(EduForumReplyDTO dto) {
        EduForumReply forumReply = new EduForumReply();
        BeanUtils.copyProperties(dto, forumReply);
        this.save(forumReply);
        
        EduForumReplyVO vo = new EduForumReplyVO();
        BeanUtils.copyProperties(forumReply, vo);
        return vo;
    }
    
    @Override
    public EduForumReplyVO getForumReplyById(Long id) {
        EduForumReply forumReply = this.getById(id);
        if (forumReply == null) {
            return null;
        }
        
        EduForumReplyVO vo = new EduForumReplyVO();
        BeanUtils.copyProperties(forumReply, vo);
        return vo;
    }
    
    @Override
    public EduForumReplyVO updateForumReply(EduForumReplyDTO dto) {
        EduForumReply forumReply = new EduForumReply();
        BeanUtils.copyProperties(dto, forumReply);
        this.updateById(forumReply);
        
        EduForumReplyVO vo = new EduForumReplyVO();
        BeanUtils.copyProperties(forumReply, vo);
        return vo;
    }
    
    @Override
    public boolean deleteForumReply(Long id) {
        return this.removeById(id);
    }
    
    @Override
    public List<EduForumReplyVO> getForumReplyList() {
        List<EduForumReply> forumReplyList = this.list();
        return forumReplyList.stream().map(forumReply -> {
            EduForumReplyVO vo = new EduForumReplyVO();
            BeanUtils.copyProperties(forumReply, vo);
            return vo;
        }).collect(Collectors.toList());
    }
}